package com.leyou.item.web;

import com.leyou.common.dto.*;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.GoodService;
import com.leyou.item.service.GoodServiceSku;
import com.leyou.item.service.ParamService;
import com.leyou.item.service.SpuDetailService;
import com.sun.xml.internal.org.jvnet.fastinfoset.stax.LowLevelFastInfosetStreamWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodController {
    @Autowired
    private GoodService goodService;

    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private GoodServiceSku serviceSku;

    @Autowired
    private ParamService paramService;

    //分页查询spu
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> findGoodPage(
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "page", defaultValue = "1") Long page,
            @RequestParam(value = "rows", defaultValue = "5") Long rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable
    ) {
        return ResponseEntity.ok(goodService.findGoodPage(brandId, categoryId, id, page, rows, saleable));
    }

    //批量查询sku
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> QueryListSku(@RequestParam("ids") List<Long> ids) {
        //serviceSku
        return ResponseEntity.ok(serviceSku.QueryListSku(ids));
    }

    //根据spuID查询sku的集合
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> QuerySkuBySpuId(@RequestParam("id") Long id) {

        return ResponseEntity.ok(serviceSku.QuerySkuBySpuId(id));
    }

    //根据id查询商品详情
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> QuerySpuDetails(@RequestParam("id")
                                                           Long id){
      return ResponseEntity.ok(spuDetailService.QuerySpuDetails(id));
    }
    //根据spuId查询spu的所有规格参数值
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> QuerySpectParam(@RequestParam("id") Long id,
                                                              @RequestParam(value = "searching",required = false) Boolean searching
                                                              ){
        return ResponseEntity.ok(goodService.QuerySpectParam(id,searching));
    }
    //根据id查询spu及sku、spuDetail等
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> QuerySpuAndSkuAndSpuDetails(@PathVariable("id") Long id){

        return ResponseEntity.ok(goodService.QuerySpuAndSkuAndSpuDetails(id));
    }
    //根据id查询商品
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> QueryGoods(@PathVariable("id") Long id){

        return ResponseEntity.ok(new SpuDTO(goodService.getById(id)));
    }
    //新增商品
    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO){
        goodService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //修改商品上下架
    @PutMapping("/saleable")
    public ResponseEntity<Void> modifyGoods(@RequestParam(value = "id",required = false) Long id,
                                            @RequestParam(value = "saleable",required = false) Boolean saleable){
         goodService.modifyGoods(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //修改商品
    @PutMapping("/spu")
    public ResponseEntity<Void> modifyGood(@RequestBody SpuDTO spuDTO){
        goodService.modifyGood(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
